﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace REPORT.DAL
{
    class ClsConnection
    {
        private string sName;
        private string dbName;
        private bool iSecurity;
        private string uid;
        private string pwd;
        protected SqlConnection _sqlConn;
        protected String _tableName = "";

    //Khai bao cac property
    public string ServerName
    {
        get
        {
            return sName;
        }
        set
        {
            sName = value;
        }
    }

    public string DatabaseName
    {
        get
        {
            return dbName;
        }
        set
        {
            dbName = value;
        }
    }

    public bool IntegratedSecurity
    {
        get
        {
            return iSecurity;
        }
        set
        {
            iSecurity = value;
        }
    }

    public string UserName
    {
        get
        {
            return uid;
        }
        set
        {
            uid = value;
        }
    }

    public string Password
    {
        get
        {
            return pwd;
        }
        set
        {
            pwd = value;
        }
    }

	public ClsConnection()
	{
        sName = "";
        dbName = "";
        iSecurity = true;
        uid = "";
        pwd = "";
	}
    public ClsConnection(string sName, string dbName, bool iSecurity, string uid = "", string pwd = "")
    {
        this.sName = sName;
        this.dbName = dbName;
        this.iSecurity = iSecurity;
        this.uid = uid;
        this.pwd = pwd;
    }
    public string getConnectionString()
    {
        SqlConnectionStringBuilder strConn = new SqlConnectionStringBuilder();
        strConn.DataSource = ServerName;
        strConn.InitialCatalog = DatabaseName;
        if (IntegratedSecurity)
            strConn.IntegratedSecurity = IntegratedSecurity;
        else
        {
            strConn.UserID = UserName;
            strConn.Password = Password;
        }
        //Tra ve chuoi ket noi
        return strConn.ConnectionString;
    }
    protected SqlDataReader exportDataToDatareader(string cols, string cri)
    {
        try
        {
            string sql = string.Format("SELECT {0} FROM {1} {2}", (cols.Length == 0) ? " * " : cols, _tableName, (cri.Length == 0) ? "" : " WHERE " + cri);
            SqlCommand sqlComm = new SqlCommand();
            sqlComm.Connection = _sqlConn;
            sqlComm.CommandType = System.Data.CommandType.Text;
            sqlComm.CommandText = sql;
            return sqlComm.ExecuteReader();
        }
        catch
        {
            return null;
        }
    }
    protected SqlDataReader exportDataToDatareader(string sql)
    {
        try
        {
            SqlCommand sqlComm = new SqlCommand();
            sqlComm.Connection = _sqlConn;
            sqlComm.CommandType = System.Data.CommandType.Text;
            sqlComm.CommandText = sql;
            return sqlComm.ExecuteReader();
        }
        catch
        {
            return null;
        }
    }
    }
}
